/**
 * Created by shc@lierda.com on 2017/1/11.
 */
var DB =  require('../db/db');

/**根据获取用户信息*/
exports.getLoginById= function(req,res,msg){
    var LoginMobile = req.body.LoginMobile;
    var LoginAccount = req.body.LoginAccount;
    var LoginPassword = req.body.LoginPassword;
    //手机号登录获取数据
    if(LoginMobile){
        var sql="SELECT * from k_user where phone ='"+LoginMobile+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----" + JSON.stringify(list));
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }
    //帐号密码登录获取数据
    else if(LoginAccount && LoginPassword) {
        var sql = "SELECT * from k_user where account='" + LoginAccount + "' and password='" + LoginPassword + "'";
        DB.query(sql, function (er, list) {
            if (!er) {
                msg.success = true;
                msg.data = list;
                res.json(msg);
            } else {
                msg.msg = er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
};
/**注册添加用户信息*/
exports.insertUserInfo=function(req,res,msg){
    var registerUId = req.body.registerUId;
    var registerAccount = req.body.registerAccount;
    var registerPassword = req.body.registerPassword;
    var registerMobile = req.body.registerMobile;
    if(registerUId){
        var sql = "INSERT INTO k_user(id,account,password,phone) VALUES ('"
            +registerUId+"','"
            +registerAccount+"','"
            +registerPassword+"','"
            +registerMobile+"')";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
}

/**更新头像*/
exports.headPortrait = function(req,res,msg){
    var userId = req.body.userId;
    var photo = req.body.photo;
    var sql="UPDATE k_user SET photo='"+photo+"' WHERE id ='"+userId+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**修改昵称*/
exports.nickname = function(req,res,msg){
    var userId = req.body.userId;
    var nickname = req.body.nickname;
    var sql="UPDATE k_user SET nickname='"+nickname+"' WHERE id ='"+userId+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**获取用户积分收支信息*/
exports.getPointInfoByUserId = function(req,res,msg){
    var userId = req.body.userId;
    var point_status = req.body.point_status;
    var sql="select point,point_status,point_desc,unix_timestamp(time) as time FROM k_point WHERE user_id ='"+userId+"' AND point_status ='"+ point_status +"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
